package valve_test.drawning;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.sql.SQLException;
import java.util.*;
import java.util.List;

import valve_test.DAO.Factory;
import valve_test.forms.graph_valves;
import valve_test.logic.*;

/**
 * Created with IntelliJ IDEA.
 * User: Shinigami
 * Date: 17.06.14
 * Time: 17:45
 * To change this template use File | Settings | File Templates.
 */
public class graph_pane extends JPanel {
    //int fl = 0;
    public static List<valves> vs = null;
    public static List<ierarchy> ies = null;
    public static List<visual> vis = null;
    private static int x_offset = 0;
    private static int y_offset = 0;
    private static int elem_per_line = 6;
    private static List<Color> cl = null;
    private static ImageIcon cist_o_icon = new ImageIcon("cist2.png");
    private static ImageIcon cist_i_icon = new ImageIcon("cist1.png");
    private static ImageIcon valve_icon = new ImageIcon("kran1_1.jpg");

    public static void setCist_o_icon(BufferedImage icon){
        cist_o_icon = new ImageIcon(icon);
    }

    public static ImageIcon getCist_o_icon(){
        return cist_o_icon;
    }

    public static void setCist_i_icon(BufferedImage icon){
        cist_i_icon = new ImageIcon(icon);
    }

    public static ImageIcon getCist_i_icon(){
        return cist_i_icon;
    }

    public static void setValve_icon(BufferedImage icon){
        valve_icon = new ImageIcon(icon);
    }

    public static ImageIcon getValve_icon(){
        return valve_icon;
    }

    public static int getEpL(){
        return elem_per_line;
    }

    public static void setEpL(int EpL){
        elem_per_line = EpL;
    }

    public static int getX_offset(){
        return x_offset;
    }

    public static void setX_offset(int x_o){
        x_offset = x_o;
    }

    public static void setY_offset(int y_o){
        y_offset = y_o;
    }

    public static int getY_offset(){
        return y_offset;
    }

    private void turning(final String temp_str){
        try{
            valves v = new valves();
            for (int j = 0; j < vs.size(); j++){
                if (temp_str.equals(vs.get(j).getv_name())){
                    if (vs.get(j).getState()) vs.get(j).setState(false);
                    else vs.get(j).setState(true);
                    v = vs.get(j);
                    break;
                }
            }
            Factory.getInstance().getValvesDAO().updateValve(v);
            for (int i = 0; i < vis.size(); i++){
                if (vis.get(i).getV_name().equals(temp_str)){
                    if (vis.get(i).getV_state()) vis.get(i).setV_state(false);
                    else vis.get(i).setV_state(true);
                }
            }
            this.repaint();
        }
        catch (SQLException ex){
            //
        }
    }

    private void init_visual(){
        int m_i = 0, m_o = 0;
        Point p1 = new Point();
        if (vis != null)
            vis.clear();
        else vis = new ArrayList<visual>();
        for (int j = 0; j < vs.size(); j++){
            visual temp_vis = new visual();
            temp_vis.setV_id(Integer.parseInt(vs.get(j).getv_id().toString()));
            temp_vis.setV_name(vs.get(j).getv_name());
            temp_vis.setV_state(vs.get(j).getState());
            if (vs.get(j).getinput() == 0) temp_vis.setV_type(1);
            else if (vs.get(j).getoutput() == 0) temp_vis.setV_type(3);
            else temp_vis.setV_type(2);
            vis.add(temp_vis);
        }

        for (int j = 0; j < vis.size(); j++){
            if (vis.get(j).getV_type() == 1) {
                vis.get(j).setLevel(p1);
                p1.x++;
                if (p1.x == elem_per_line){
                    p1.x = 0;
                    p1.y++;
                }
            }
        }
        p1.x = 0;
        p1.y++;
        for (int j = 0; j < vis.size(); j++){
            if (vis.get(j).getV_type() == 2){
                vis.get(j).setLevel(p1);
                p1.x++;
                if (p1.x == elem_per_line){
                    p1.x = 0;
                    p1.y++;
                }
            }
        }
        p1.x = 0;
        p1.y++;
        for (int j = 0; j < vis.size(); j++){
            if (vis.get(j).getV_type() == 3){
                vis.get(j).setLevel(p1);
                p1.x++;
                if (p1.x == elem_per_line){
                    p1.x = 0;
                    p1.y++;
                }
            }
        }
        for (int j = 0; j < vis.size(); j++){
            int[] temp_mas_o = new int[1];
            String[] temp_mas_i = new String[1];
            for (int k = 0; k < vs.size(); k++){
                if (vs.get(k).getv_id() == vis.get(j).getV_id()){
                    if (vs.get(k).getoutput() > 0){
                        temp_mas_o = new int[Integer.parseInt(vs.get(k).getoutput().toString())];
                        for (int q = 0; q < Integer.parseInt(vs.get(k).getoutput().toString()); q++)
                            temp_mas_o[q] = -1;
                    }
                    else {
                        temp_mas_o = new int[1];
                        temp_mas_o[0] = -1;
                    }
                    if (vs.get(k).getinput() > 0){
                        temp_mas_i = new String[Integer.parseInt(vs.get(k).getinput().toString())];
                        for (int q = 0; q < Integer.parseInt(vs.get(k).getinput().toString()); q++)
                            temp_mas_i[q] = "-1";
                    }
                    else {
                        temp_mas_i = new String[1];
                        temp_mas_i[0] = new String("-1");
                    }
                }
            }
            for (int k = 0; k < ies.size(); k++){
                if (ies.get(k).getP_id() == vis.get(j).getV_id()){
                    for (int l = 0; l < vis.size(); l++){
                        if (ies.get(k).getC_id() == vis.get(l).getV_id()) {
                            temp_mas_o[Integer.parseInt(ies.get(k).getP_o().toString())-1] = l;
                            break;
                        }
                    }
                }
                if (ies.get(k).getC_id() == vis.get(j).getV_id()) {
                    for (int l = 0; l < vis.size(); l++){
                        if (ies.get(k).getP_id() == vis.get(l).getV_id()) {
                            temp_mas_i[Integer.parseInt(ies.get(k).getC_i().toString())-1] = String.valueOf(l) + " " + String.valueOf(ies.get(k).getP_o()-1);
                            break;
                        }
                    }
                }
            }
            vis.get(j).setMas_out(temp_mas_o);
            vis.get(j).setMas_in(temp_mas_i);
        }

        Random numGen = new Random();
        cl = new ArrayList<Color>();
        for (int i = 0; i < vis.size(); i++){
            if (vis.get(i).getMas_out() != null){
                for (int j = 0; j < vis.get(i).getMas_out().length; j++){
                    cl.add(new Color(numGen.nextInt(256), numGen.nextInt(256), numGen.nextInt(256)));
                }
            }
        }
    }

    private void doDrawing(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        this.removeAll();
        try{
            if (vs == null || ies == null){
                vs = Factory.getInstance().getValvesDAO().getAllValves();
                ies = Factory.getInstance().getIerarchyDAO().getAllIerarchies();
                init_visual();
            }

            this.setLayout(null);
            int x_interval = 0;
            int y_interval = (elem_per_line*50+10)/2;

            for (int i = 0; i < vis.size(); i++){
                if (vis.get(i).getLevel().y > x_interval) x_interval = vis.get(i).getLevel().y;
            }
            int max_col = x_interval;

            int[] diff_line1 = new int[x_interval+1];
            for (int i = 0; i < x_interval+1 ; i++)
                diff_line1[i] = 0;
            int[] diff_line2 = new int[x_interval+1];
            for (int i = 0; i < x_interval+1 ; i++)
                diff_line2[i] = 0;
            int[] diff_col = new int[elem_per_line];
            for (int i = 0; i < elem_per_line ; i++)
                diff_col[i] = 0;

            x_interval = 20+(x_interval-1)*25;

            Dimension p_size = this.getSize();
            int max_line = 0;
            for (int i = 0; i < vis.size(); i++)
                if (vis.get(i).getLevel().x > max_line)
                    max_line = vis.get(i).getLevel().x;
            int max_x = 0, max_y = 0;

            for (int i = 0; i < vis.size(); i++){
                JLabel lbl = new JLabel();
                lbl.setSize(40,40);
                lbl.setLocation(10+vis.get(i).getLevel().x*(x_interval+40)-x_offset, 10+vis.get(i).getLevel().y*(y_interval+40)-y_offset);
                if (vis.get(i).getV_state())
                    g2d.setColor(new Color(0,255,0));
                else g2d.setColor(new Color(255,0,0));
                g2d.fillRect(10+vis.get(i).getLevel().x*(x_interval+40)-2-x_offset, 10+vis.get(i).getLevel().y*(y_interval+40)-2-y_offset,44,44);
                if (vis.get(i).getV_type() == 1) lbl.setIcon(cist_o_icon);
                if (vis.get(i).getV_type() == 2) lbl.setIcon(valve_icon);
                if (vis.get(i).getV_type() == 3) lbl.setIcon(cist_i_icon);
                lbl.setToolTipText(vis.get(i).getV_name());
                final String temp_str= vis.get(i).getV_name();
                lbl.addMouseListener(new MouseListener() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        turning(temp_str);
                    }

                    @Override
                    public void mousePressed(MouseEvent e) {
                        //To change body of implemented methods use File | Settings | File Templates.
                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {
                        //To change body of implemented methods use File | Settings | File Templates.
                    }

                    @Override
                    public void mouseEntered(MouseEvent e) {
                        //To change body of implemented methods use File | Settings | File Templates.
                    }

                    @Override
                    public void mouseExited(MouseEvent e) {
                        //To change body of implemented methods use File | Settings | File Templates.
                    }
                });
                this.add(lbl);
            }
            //Random numGen = new Random();
            int cl_num = 0;
            for (int i = 0; i < vis.size(); i++){
                if (vis.get(i).getV_type() != 3){
                    for (int j = 0; j < vis.get(i).getMas_out().length; j++)
                    {
                        if (vis.get(i).getMas_out()[j] != -1){
                            int temp = vis.get(i).getMas_out()[j];
                            for (int k = 0; k < vis.get(temp).getMas_in().length; k++){
                                if ((String.valueOf(i) + " " + String.valueOf(j)).equals(vis.get(temp).getMas_in()[k])){
                                    int temp_point;
                                    diff_line1[vis.get(i).getLevel().y]++;
                                    diff_col[vis.get(i).getLevel().x]++;
                                    diff_line2[vis.get(temp).getLevel().y]++;
                                    temp_point = 10+vis.get(i).getLevel().x*(x_interval+40)-x_offset+5*(j+1);
                                    int[] xp = new int[6];
                                    int[] yp = new int[6];
                                    xp[0] = temp_point;
                                    xp[1] = temp_point;
                                    temp_point += 45+5*diff_col[vis.get(i).getLevel().x]-5*(j+1);
                                    xp[2] = temp_point;
                                    xp[3] = temp_point;
                                    temp_point = 10+vis.get(temp).getLevel().x*(x_interval+40)-x_offset+5*(k+1);
                                    xp[4] = temp_point;
                                    xp[5] = temp_point;
                                    temp_point = 10+vis.get(i).getLevel().y*(y_interval+40)-y_offset+40;
                                    yp[0] = temp_point;
                                    temp_point += 5+5*diff_line1[vis.get(i).getLevel().y];
                                    yp[1] = temp_point;
                                    yp[2] = temp_point;
                                    temp_point = 10+vis.get(temp).getLevel().y*(y_interval+40)-y_offset-5-5*diff_line2[vis.get(temp).getLevel().y];
                                    yp[3] = temp_point;
                                    yp[4] = temp_point;
                                    temp_point += 5+5*diff_line2[vis.get(temp).getLevel().y];
                                    yp[5] = temp_point;
                                    cl_num++;
                                    //Color col = new Color(numGen.nextInt(256), numGen.nextInt(256), numGen.nextInt(256));
                                    g2d.setColor(cl.get(cl_num));
                                    g2d.setStroke(new BasicStroke(3));
                                    g2d.drawPolyline(xp,yp,6);
                                }
                            }
                        }
                    }
                }
            }
            if (p_size.width < (10+max_line*(x_interval+40)+42))
                max_x = (10+max_line*(x_interval+40)+52+5*diff_col[max_col]) - p_size.width;
            else max_x = 0;
            if (p_size.height < (10+max_col*(y_interval+40)+42))
                max_y = (10+max_col*(y_interval+40)+52) - p_size.height;
            else max_y = 0;
            graph_valves.GraphValves.refresh(max_x,max_y);

            /*int val_num = 0, cist_o = 0, cist_i = 0;
            int[] mas1 = new int[vs.size()*2];
            int[] mas2 = new int[vs.size()*2];
            for (int j = 0; j < vs.size()*2; j++){
                mas1[j] = 0;
                mas2[j] = 0;
            }

            for (int i = 0; i < vs.size(); i++){
                if (vs.get(i).getoutput() == 0) cist_i++;
                if (vs.get(i).getinput() == 0) cist_o++;
                if ((vs.get(i).getoutput() != 0)&&(vs.get(i).getinput() != 0)) val_num++;
            }

            int offset_cist_i = 0;
            int offset_cist_o = 0;
            if (cist_i > val_num) offset_cist_i = (cist_i - val_num)*140-80;
            if (cist_o > val_num) offset_cist_o = (cist_o - val_num)*140-80;

            for (int i = 0; i < cist_o; i++){                                           //сливные цистерны
                JLabel lbl = new JLabel();
                lbl.setSize(40,57);
                if (cist_o % 2 == 0 ) {
                    int st = (110+50*val_num+100*(val_num-1))/2+(cist_o/2-1)*140-i*140+offset_cist_o;
                    lbl.setLocation(st, 13);
                }
                else{
                    int st = (-10+50*val_num+100*(val_num-1))/2+cist_o/2*140-i*140+offset_cist_o;
                    lbl.setLocation(st, 13);
                }
                lbl.setIcon(cist_o_icon);
                lbl.setVisible(true);
                int a = 0;
                Long temp = 0L;
                for (int j = 0; j < vs.size(); j++)
                    if (vs.get(j).getinput() == 0){
                        if (a == i){
                            lbl.setToolTipText(vs.get(j).getv_name());
                            temp = vs.get(j).getv_id();
                        }
                        a++;
                    }
                final Long finalTemp = temp;
                lbl.addMouseListener(new MouseListener() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        //new show_params(finalTemp);
                    }

                    @Override
                    public void mousePressed(MouseEvent e) {
                        //To change body of implemented methods use File | Settings | File Templates.
                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {
                        //To change body of implemented methods use File | Settings | File Templates.
                    }

                    @Override
                    public void mouseEntered(MouseEvent e) {
                        //To change body of implemented methods use File | Settings | File Templates.
                    }

                    @Override
                    public void mouseExited(MouseEvent e) {
                        //To change body of implemented methods use File | Settings | File Templates.
                    }
                });
                this.add(lbl);
            }

            for (int i = 0; i < cist_i; i++){                                           //наливные цистерны
                JLabel lbl = new JLabel();
                lbl.setSize(40,57);
                if (cist_i % 2 == 0 ) {
                    int st = (110+50*val_num+100*(val_num-1))/2+(cist_i/2-1)*140-i*140+offset_cist_i;
                    lbl.setLocation(st, 423);
                }
                else{
                    int st = (-10+50*val_num+100*(val_num-1))/2+cist_i/2*140-i*140+offset_cist_i;
                    lbl.setLocation(st, 423);
                }
                lbl.setIcon(cist_i_icon);
                lbl.setVisible(true);
                int a = 0;
                Long temp = 0L;
                for (int j = 0; j < vs.size(); j++)
                    if (vs.get(j).getoutput() == 0){
                        if (a == i){
                            lbl.setToolTipText(vs.get(j).getv_name());
                            mas1[i] = vs.get(j).getv_id().intValue();
                            temp = vs.get(j).getv_id();
                        }
                        a++;
                    }
                final Long finalTemp = temp;
                lbl.addMouseListener(new MouseListener() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                       // new show_params(finalTemp);
                    }

                    @Override
                    public void mousePressed(MouseEvent e) {
                        //To change body of implemented methods use File | Settings | File Templates.
                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {
                        //To change body of implemented methods use File | Settings | File Templates.
                    }

                    @Override
                    public void mouseEntered(MouseEvent e) {
                        //To change body of implemented methods use File | Settings | File Templates.
                    }

                    @Override
                    public void mouseExited(MouseEvent e) {
                        //To change body of implemented methods use File | Settings | File Templates.
                    }
                });
                this.add(lbl);
            }

            for (int i = 0; i < val_num; i++){                                            //вентили
                JLabel lbl = new JLabel();
                lbl.setSize(50,50);
                lbl.setLocation(10+i*50+100*i, 223);
                lbl.setIcon(valve_icon);
                lbl.setVisible(true);
                int a = 0;
                Long temp = 0L;
                for (int j = 0; j < vs.size(); j++)
                    if ((vs.get(j).getoutput() != 0)&&(vs.get(j).getinput() != 0)){
                        if (a == i){
                            lbl.setToolTipText(vs.get(j).getv_name());
                            mas2[i] = vs.get(j).getv_id().intValue();
                            temp = vs.get(j).getv_id();
                        }
                        a++;
                    }
                final Long finalTemp = temp;
                lbl.addMouseListener(new MouseListener() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        //new show_params(finalTemp);
                    }

                    @Override
                    public void mousePressed(MouseEvent e) {
                        //To change body of implemented methods use File | Settings | File Templates.
                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {
                        //To change body of implemented methods use File | Settings | File Templates.
                    }

                    @Override
                    public void mouseEntered(MouseEvent e) {
                        //To change body of implemented methods use File | Settings | File Templates.
                    }

                    @Override
                    public void mouseExited(MouseEvent e) {
                        //To change body of implemented methods use File | Settings | File Templates.
                    }
                });
                this.add(lbl);
            }

            Component[] comp= this.getComponents();
            int sliv_c = 0;
            int dif_line1 = 0, dif_line2 = 0, dif_line3 = 0;
            boolean fl1 = false;
            int rd = 0, gr = 0, bl = 0;
            for (Component com : comp){
                if (com instanceof JLabel){
                    JLabel l = (JLabel) com;
                    valves temp_val = null;
                    for (int i = 0; i < vs.size(); i++){
                        if (vs.get(i).getv_name().equals(l.getToolTipText()))
                            temp_val = vs.get(i);
                    }
                    //Factory.getInstance().getValvesDAO().getValveByName(l.getToolTipText());
                    int q = 0;//для вывода с вентилей
                    for (int i = 0; i < ies.size(); i++){
                        if (ies.get(i).getP_id() == temp_val.getv_id()){
                            if (temp_val.getinput() == 0){       //трубы со сливных цистерн
                                fl1 = true;
                                if (cist_o % 2 == 0){
                                    q++;
                                    int p1 = (110+50*val_num+100*(val_num-1))/2+(cist_o/2-1)*140-sliv_c*140+5*q+offset_cist_o;
                                    int dest_num = 0;
                                    dif_line1++;
                                    for (int j = 0; j < vs.size(); j++){
                                        if ((vs.get(j).getoutput() != 0)&&(vs.get(j).getinput() != 0)){
                                            dest_num++;
                                            if (vs.get(j).getv_id() == ies.get(i).getC_id()) {
                                                break;
                                            }
                                        }
                                    }
                                    mas2[vs.size()+dest_num-1]++;
                                    int p2 = 10+150*(dest_num-1)+5*mas2[vs.size()+dest_num-1];
                                    int[] xp = {p1,p1,p2,p2};
                                    int[] yp = {70,105+5*dif_line1,105+5*dif_line1,223};
                                    rd += 100;
                                    if (rd > 255){
                                        rd -= 255;
                                        gr += 100;
                                        if (gr > 255){
                                            gr -= 255;
                                            bl += 100;
                                            if (bl > 255){
                                                bl -= 255;
                                                rd += 100;
                                            }
                                        }
                                    }
                                    Color col = new Color(rd, gr, bl);
                                    g2d.setColor(col);
                                    g2d.setStroke(new BasicStroke(3));
                                    g2d.drawPolyline(xp,yp,4);
                                }
                                else{
                                    q++;
                                    dif_line1++;
                                    int p1 = (-10+50*val_num+100*(val_num-1))/2+cist_o/2*140-sliv_c*140+5*q+offset_cist_o;
                                    int dest_num = 0;
                                    for (int j = 0; j < vs.size(); j++){
                                        if ((vs.get(j).getoutput() != 0)&&(vs.get(j).getinput() != 0)){
                                            dest_num++;
                                            if (vs.get(j).getv_id() == ies.get(i).getC_id()) {
                                                break;
                                            }
                                        }
                                    }
                                    mas2[vs.size()+dest_num-1]++;
                                    int p2 = 10+150*(dest_num-1)+5*mas2[vs.size()+dest_num-1];
                                    int[] xp = {p1,p1,p2,p2};
                                    int[] yp = {70,105+5*dif_line1,105+5*dif_line1,223};
                                    rd += 100;
                                    if (rd > 255){
                                        rd -= 255;
                                        gr += 100;
                                        if (gr > 255){
                                            gr -= 255;
                                            bl += 100;
                                            if (bl > 255){
                                                bl -= 255;
                                                rd += 100;
                                            }
                                        }
                                    }
                                    Color col = new Color(rd, gr, bl);
                                    g2d.setColor(col);
                                    g2d.setStroke(new BasicStroke(3));
                                    g2d.drawPolyline(xp,yp,4);
                                }
                            }
                            else{
                                for (int j = 0; j < vs.size(); j++){
                                    if (vs.get(j).getv_id() == ies.get(i).getC_id()){
                                        if ((vs.get(j).getoutput() != 0)&&(vs.get(j).getinput() != 0)){             // трубы м/у вентилями
                                            q++;
                                            dif_line2++;
                                            int dest_num1 = 0, dest_num2 = 0;
                                            for (int k = 0; k < vs.size(); k++){
                                                if ((vs.get(k).getoutput() != 0)&&(vs.get(k).getinput() != 0)){
                                                    dest_num1++;
                                                    if (vs.get(k).getv_id() == ies.get(i).getC_id()) {
                                                        break;
                                                    }
                                                }
                                            }
                                            for (int k = 0; k < vs.size(); k++){
                                                if ((vs.get(k).getoutput() != 0)&&(vs.get(k).getinput() != 0)){
                                                    dest_num2++;
                                                    if (vs.get(k).getv_id() == ies.get(i).getP_id()) {
                                                        break;
                                                    }
                                                }
                                            }
                                            int p1 = 10+150*(dest_num2-1)+5*q;
                                            int p2 = 10+150*(dest_num1-1)+5*q;
                                            int[] xp = {p1,p1,p1+55,p1+55,p2,p2};
                                            int[] yp = {273,285+5*dif_line2,285+5*dif_line2,195+5*dif_line2,195+5*dif_line2,223};
                                            rd += 100;
                                            if (rd > 255){
                                                rd -= 255;
                                                gr += 100;
                                                if (gr > 255){
                                                    gr -= 255;
                                                    bl += 100;
                                                    if (bl > 255){
                                                        bl -= 255;
                                                        rd += 100;
                                                    }
                                                }
                                            }
                                            Color col = new Color(rd, gr, bl);
                                            g2d.setColor(col);
                                            g2d.setStroke(new BasicStroke(3));
                                            g2d.drawPolyline(xp,yp,6);
                                        }
                                        else{
                                            if (vs.get(j).getoutput() == 0){                    // трубы к наливным цистернам
                                                if (cist_i % 2 == 0){
                                                    q++;
                                                    dif_line3++;
                                                    int dest_num1 = 0;
                                                    for (int k = 0; k < vs.size(); k++){
                                                        if ((vs.get(k).getoutput() != 0)&&(vs.get(k).getinput() != 0)){
                                                            dest_num1++;
                                                            if (vs.get(k).getv_id() == ies.get(i).getP_id()) {
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    int naliv_num = 0;
                                                    for (int k = 0; k < vs.size(); k++)
                                                        if (vs.get(j).getv_id().intValue() == mas1[k]){
                                                            naliv_num = k;
                                                            mas1[vs.size()+k-1]++;
                                                            break;
                                                        }
                                                    int p1 = 10+150*(dest_num1-1)+5*q;
                                                    int p2 = (110+50*val_num+100*(val_num-1))/2+(cist_i/2-1)*140-naliv_num*140+5*mas1[vs.size()+naliv_num-1]+offset_cist_i;
                                                    int[] xp = {p1,p1,p2,p2};
                                                    int[] yp = {273,350+5*dif_line3,350+5*dif_line3,423};
                                                    rd += 100;
                                                    if (rd > 255){
                                                        rd -= 255;
                                                        gr += 100;
                                                        if (gr > 255){
                                                            gr -= 255;
                                                            bl += 100;
                                                            if (bl > 255){
                                                                bl -= 255;
                                                                rd += 100;
                                                            }
                                                        }
                                                    }
                                                    Color col = new Color(rd, gr, bl);
                                                    g2d.setColor(col);
                                                    g2d.setStroke(new BasicStroke(3));
                                                    g2d.drawPolyline(xp,yp,4);
                                                }
                                                else {
                                                    q++;
                                                    dif_line3++;
                                                    int dest_num1 = 0;
                                                    for (int k = 0; k < vs.size(); k++){
                                                        if ((vs.get(k).getoutput() != 0)&&(vs.get(k).getinput() != 0)){
                                                            dest_num1++;
                                                            if (vs.get(k).getv_id() == ies.get(i).getP_id()) {
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    int naliv_num = 0;
                                                    for (int k = 0; k < vs.size(); k++)
                                                        if (vs.get(j).getv_id().intValue() == mas1[k]){
                                                            naliv_num = k;
                                                            mas1[vs.size()+k-1]++;
                                                            break;
                                                        }
                                                    int p1 = 10+150*(dest_num1-1)+5*q;
                                                    int p2 = (-10+50*val_num+100*(val_num-1))/2+cist_i/2*140-naliv_num*140+5*mas1[vs.size()+naliv_num-1]+offset_cist_i;
                                                    int[] xp = {p1,p1,p2,p2};
                                                    int[] yp = {273,350+5*dif_line3,350+5*dif_line3,423};
                                                    rd += 100;
                                                    if (rd > 255){
                                                        rd -= 255;
                                                        gr += 100;
                                                        if (gr > 255){
                                                            gr -= 255;
                                                            bl += 100;
                                                            if (bl > 255){
                                                                bl -= 255;
                                                                rd += 100;
                                                            }
                                                        }
                                                    }
                                                    Color col = new Color(rd, gr, bl);
                                                    g2d.setColor(col);
                                                    g2d.setStroke(new BasicStroke(3));
                                                    g2d.drawPolyline(xp,yp,4);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (fl1){
                        sliv_c++;
                        fl1 = false;
                    }
                }
            }
            fl++; */
        }catch (SQLException ex){
            //
        }

        /*int[] xp = {606, 606, 16, 16};
        int[] yp = {70, 110, 110, 225};
        Color col = new Color(255, 0, 0);
        g2d.setColor(col);
        g2d.setStroke(new BasicStroke(5));
        g2d.drawPolyline(xp,yp,4);
        int[] xp1 = {600, 600, 10, 10};
        int[] yp1 = {70, 115, 115, 230};
        col = new Color(0, 0, 255);
        g2d.setColor(col);
        g2d.drawPolyline(xp1,yp1,4);*/
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        doDrawing(g);
    }
}
